package com.qingxin.wuye.action;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.wuye.service.SysUserBuildingRelService;
import com.qingxin.wuye.service.SysUserGardenRelService;

@Controller
@RequestMapping("/userGarden")
public class UserGardenAction extends BaseAction {

	@Autowired
	private SysUserGardenRelService userGardenRelService;
	@Autowired
	private SysUserBuildingRelService userBuildingRelService;
	
	
	/**
	 * 为用户设置小区关联关系
	 * @param model
	 * @param userid
	 * @return
	 */
	@RequestMapping(value = "/setGardens.do", method = RequestMethod.GET)
	public String setGardens(ModelMap model, String userid) {
		model.put("userid", userid);
		return "sys/user/setGardens";
	}
	
	
	/**
	 * 获取小区-楼宇树数据
	 * @param userid
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getSetGardenData.json", method = {RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object getSetGardenData(String userid, HttpServletRequest request){
		if(StringUtils.isEmpty(userid)){
			return new ArrayList<Object>();
		}
		return userBuildingRelService.getSetGardenData(userid, getLoginUser(request));
	}
	
	
	/**
	 * 保存用户-小区-楼宇关系
	 * @param userid
	 * @param parkid
	 * @param blockid
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/saveUserGardenRels.json", method = RequestMethod.POST)
	@ResponseBody
	public Object saveUserGardenRels(String userid, String[] gardenid, String[] buildingid, HttpServletRequest request){
		if(StringUtils.isEmpty(userid)){
			return new SysError("参数错误");
		}
		
		LoginUser loginUser = getLoginUser(request);
		userGardenRelService.saveUserGardenRels(userid, gardenid, loginUser);
		userBuildingRelService.saveUserBuildingRels(userid, buildingid, loginUser);
		return null;
	}
	
	
}
